﻿package app.cyancdesign {
	
	import flash.events.*;
	import flash.display.*;
	import flash.net.URLRequest;
	import flash.events.TouchEvent;
	import app.cyancdesign.MainNav;
	import app.cyancdesign.MainButton;
	import app.cyancdesign.ResetSwitch;
	import fl.transitions.Tween;
	import fl.transitions.TweenEvent;
	import fl.transitions.easing.*;
	import com.exanimo.transitions.GCSafeTween;

	
	public class MovieContainer extends MovieClip {
		
		var navigation = new MainNav();
		var resetSwitch = new ResetSwitch();
		var movieMC:MovieClip = new MovieClip();
		var ldr:Loader = new Loader();
		
			var greylines = new MainButton();
			var Squeegee = new MainButton();
			var Cradle = new MainButton();
			var RingsApp = new MainButton();
			var Physics = new MainButton();
			var GoogleMap = new MainButton();
		
		var fadeTween:GCSafeTween;
		var scaleXTween:GCSafeTween;
		var scaleYTween:GCSafeTween;
		
		public function MovieContainer():void{
			TUIO.init(this,'127.0.0.1',3000,'',false);
			addChild(movieMC);
			movieMC.alpha = 0;
			
			addChild(navigation);
			navigation.y = 768;
			addNavElements();
			
			
			ldr.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, loadProgress);
			ldr.contentLoaderInfo.addEventListener(Event.COMPLETE, loadComplete);

		}
		
		public function addNavElements():void{
			greylines.SWFurl = "GreyLines.swf";
			greylines.y = -8;
			greylines.x = 10;
			greylines.btnText.text = "Grey Lines";
			navigation.addChild(greylines);

			Squeegee.SWFurl = "Squeegee.swf";
			Squeegee.y = -8;
			Squeegee.x = 114;
			Squeegee.btnText.text = "Squeegee";
			navigation.addChild(Squeegee);

			Cradle.SWFurl = "CatsCradle.swf";
			Cradle.y = -8;
			Cradle.x = 218;
			Cradle.btnText.text = "Cats Cradle";
			navigation.addChild(Cradle);

			RingsApp.SWFurl = "Rings.swf";
			RingsApp.y = -8;
			RingsApp.x = 322;
			RingsApp.btnText.text = "Scaling Rings";
			navigation.addChild(RingsApp);

			Physics.SWFurl = "CrateTest.swf";
			Physics.y = -8;
			Physics.x = 426;
			Physics.btnText.text = "Physics";
			navigation.addChild(Physics);
			
			GoogleMap.SWFurl = "googleMaps_intro.swf";
			GoogleMap.y = -8;
			GoogleMap.x = 530;
			GoogleMap.btnText.text = "Google Map";
			navigation.addChild(GoogleMap);
			
			attachListeners();
		}
		
		public function attachListeners(){
			greylines.addEventListener(TouchEvent.MOUSE_DOWN, selectItem);
			Squeegee.addEventListener(TouchEvent.MOUSE_DOWN, selectItem);
			Cradle.addEventListener(TouchEvent.MOUSE_DOWN, selectItem);
			RingsApp.addEventListener(TouchEvent.MOUSE_DOWN, selectItem);
			Physics.addEventListener(TouchEvent.MOUSE_DOWN, selectItem);
			GoogleMap.addEventListener(TouchEvent.MOUSE_DOWN, selectItem);
		}
		public function removeListeners(){
			greylines.addEventListener(TouchEvent.MOUSE_DOWN, selectItem);
			Squeegee.addEventListener(TouchEvent.MOUSE_DOWN, selectItem);
			Cradle.addEventListener(TouchEvent.MOUSE_DOWN, selectItem);
			RingsApp.addEventListener(TouchEvent.MOUSE_DOWN, selectItem);
			Physics.addEventListener(TouchEvent.MOUSE_DOWN, selectItem);
			GoogleMap.addEventListener(TouchEvent.MOUSE_DOWN, selectItem);
		}
		
		public function selectItem(eo:TouchEvent):void{
			removeListeners();
			trace("LOADING"+eo.target.parent.SWFurl)
			var url:String = eo.target.parent.SWFurl;
			var urlReq:URLRequest = new URLRequest(url);
			ldr.load(urlReq);
			movieMC.addChild(ldr);
			var myTweenReturnNav:GCSafeTween = new GCSafeTween(movieMC, "alpha", Strong.easeIn, movieMC.alpha, 1, 2, true);

			var myTween:GCSafeTween = new GCSafeTween(navigation, "x", Strong.easeIn, navigation.x, -1324, 1.5, true);
			
			resetSwitch.alpha = 0;
			resetSwitch.x = 1022;
			resetSwitch.y = 766;
			resetSwitch.scaleX = .25;
			resetSwitch.scaleY = .25;
			addChild(resetSwitch);
			resetSwitch.addEventListener(TouchEvent.MOUSE_DOWN, fadeInReset);
			resetSwitch.addEventListener(TouchEvent.MOUSE_UP, fadeOutReset);
		}
		
		public function fadeInReset(eo:TouchEvent):void{
			trace("Touched Reset DOWN")
			fadeTween = new GCSafeTween(resetSwitch, "alpha", null, resetSwitch.alpha, 1, 3, true);
			scaleXTween = new GCSafeTween(resetSwitch, "scaleX", null, resetSwitch.scaleX, 1, 3, true);
			scaleYTween = new GCSafeTween(resetSwitch, "scaleY", null, resetSwitch.scaleY, 1, 3, true);
			fadeTween.addEventListener(TweenEvent.MOTION_FINISH, checkResetFinish);

		}
		
		public function checkResetFinish(eo:TweenEvent):void{
			trace("load wait finished!!!")
			var myTweenReturnNav:GCSafeTween = new GCSafeTween(navigation, "x", Strong.easeIn, navigation.x, 0, 1.5, true);			
			
			resetSwitch.alpha = 0;
			resetSwitch.scaleX = .25;
			resetSwitch.scaleY = .25;
			resetSwitch.removeEventListener(TouchEvent.MOUSE_DOWN, fadeInReset);
			resetSwitch.removeEventListener(TouchEvent.MOUSE_UP, fadeOutReset);
			
			removeChild(resetSwitch);
			var myTweenReturnNavBack:GCSafeTween = new GCSafeTween(movieMC, "alpha", Strong.easeIn, movieMC.alpha, .8, 2, true);
		}

		
		public function fadeOutReset(eo:TouchEvent):void{
			trace("Touched Reset UP")
			fadeTween.removeEventListener(TweenEvent.MOTION_FINISH, checkResetFinish);
			fadeTween.continueTo(0, 1);
			scaleXTween.continueTo(.25, 1);
			scaleYTween.continueTo(.25, 1);
		}

		
		function loadProgress(event:ProgressEvent):void {
			var percentLoaded:Number = event.bytesLoaded/event.bytesTotal;
			percentLoaded = Math.round(percentLoaded * 100);
			trace("Loading: "+percentLoaded+"%");
		}
		function loadComplete(event:Event):void {
			trace("Complete");
		}
		
	}
	
}